package com.yuma.advisor.controller;

import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ChatMemoryController {
    ChatClient chatClient;
    public  ChatMemoryController(@Autowired OllamaChatModel chatModel,
                      @Autowired ChatMemory chatMemory) {
        chatClient = ChatClient
                .builder(chatModel)
                .defaultAdvisors(
                        PromptChatMemoryAdvisor.builder(chatMemory).build()
                )
                .build();
    }

    @GetMapping("/chat/memory")
    public String testChatOptions() {
        chatClient.prompt()
                .user("我叫御码 ")
                .call()
                .content();

        return chatClient.prompt()
                .user("我叫什么")
                .call()
                .content();
    }

    @GetMapping("/chat/yuma")
    public String yuma() {
        chatClient.prompt()
                .user("我叫御码")
                .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID, "1"))
                .call()
                .content();
        return chatClient.prompt()
                .user("我叫什么")
                .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID, "1"))
                .call()
                .content();
    }

    @GetMapping("/chat/lijing")
    public String lijing() {
        chatClient.prompt()
                .user("我叫李晶")
                .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID,"2"))
                .call()
                .content();

        return chatClient.prompt()
                .user("我叫什么")
                .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID,"2"))
                .call()
                .content();
    }
}

